Ext.define('yxt.controller.RegisterWin', {
	extend: 'Ext.app.Controller',
	views: ['RegisterWin','LoginWin','FileUploadPhisics'],
	refs: [
	       		{ref: 'RegisterWin', selector: 'registerWin'},
	       		{ref: 'ChatWin', selector: 'chatWin'}
   	 ],
	 init: function() {
		 var me = this;
		 if(me.isInited) {
				return;
		  }
		 me.control({
			 'registerWin button[itemId=registerBtn]': {
				 click: me.doRegister
			 },
			 'chatWin textarea[name=text]': {
				 keypress: function(scope, e) {
						if((e.keyCode == e.ENTER || e.keyCode == 10)&& e.ctrlKey) {
							me.sendText();
						}
				 }
			 },
			 'chatWin button': {
				 click: function(scope) {
					 switch(scope.itemId) {
					 case 'send': me.sendText();break;
					 case 'edit': me.doEdit();break;
					 case 'delete': me.doDelete();break;
					 }
				 }
			 },
			 'fileUploadP': {
				 afterSave: function(scope, result){
						if(result.success) {
							  var content;
							  if(scope.itemId == 'pic') {
								  content = '<img height=80 src="'+result.small_url+'"/>';
								  me.sendRequest('<a target=_blank href="'+result.url+'">'+ content + '</a>');
							  }else if(scope.itemId == 'file') {
								  content = result.url.substring(result.url.lastIndexOf("/")+1);
								  me.sendRequest('<a target=_blank href="./file/download.do?filename='+ content + '">'+content+'</a>');
							  }
							  
						}	
				 }
			 }
			 
		 });
		 me.isInited = true;
	 },
	 doRegister: function() {
		 var me = this;
		 var registerWin = me.getRegisterWin();		 
		 var form = registerWin.down('form');
		 if(!form.getForm().isValid()) {
			 return;
		 }
		 var data = form.getValues();
		 if(data.password != data.password1) {
			 Ext.Msg.alert('错误', '两次密码输入不一致。');
			 return;
		 }
		 Ext.Ajax.request({
			 url: 'user/register.do',
			 params: data,
			 success: function() {
				 me.getRegisterWin().close();
				 //打开登录窗口				 
				 Ext.widget('loginWin').show();
			 }
		 });
	 },
	 sendText: function() {
		 var me = this;
		 var chatWin = me.getChatWin();
		 var textCmp = chatWin.down('textarea');
		 var text = textCmp.getValue();
		 me.sendRequest(text);
		 //清除
		 textCmp.setValue('');
		 textCmp.focus();		 
	 },
	 doEdit: function() {
		 var me = this;
		 var chatWin = me.getChatWin();
		 var selected = chatWin.down('#chat_his').getSelectionModel().getSelection();
		 if(selected.length == 0) {
			 return;
		 }
		 var rec = selected[0];
		 var win = Ext.widget('window',{
			 layout:'fit',
			 width: 338,
			 height: 175,
			 items: [{
				 xtype: 'form',
				 layout: 'fit',
				 items: {
					 xtype: 'textarea',
					 name: 'text'
				 }
			 }],
			 listeners: {
				 afterrender:function(scope) {
					 scope.down('form').loadRecord(rec);
				 }
			 },
			 buttons: [{
				 text: '确定',
				 iconCls: 'yxt-ok',
				 handler: function(btn) {
					 var form = btn.up('window').down('form');
					 var record = form.getRecord();
					 form.getForm().updateRecord(record);
					 
					 Ext.Ajax.request({
						 url: 'message/updateChatHis.do',
						 params: record.getData(),
						 success: function(response) {
							 rec.set('text', record.get('text'));
							 rec.commit();
							 btn.up('window').close();
						 }
					 });
				 }
			 },{
				 text: '取消',
				 iconCls: 'yxt-delete',
				 handler: function(btn) {
					 btn.up('window').close();
				 }
			 }]
		 });
		 win.show();
	 },
	 doDelete: function() {
		var me = this; 
		var chatWin = me.getChatWin();
		var dataview = chatWin.down('#chat_his');
		var selected = dataview.getSelectionModel().getSelection();
		if(selected.length == 0) {
			 return;
		}
		var rec = selected[0];
		Ext.Ajax.request({
			url: 'message/deleteChatHis.do',
			params: rec.getData(),
			success: function() {
				dataview.getStore().remove(rec);
			}
		});
	 },
	 sendRequest: function(text) {
		 var me = this;
		 var chatWin = me.getChatWin();
		 if(text == '') {
			 Ext.Msg.alert('提示', '发送内容不能为空！');
			 return;
		 }
		 Ext.Ajax.request({
			 url: 'message/send.do', 
			 params: {
				 text: text,
				 user_id: chatWin.user_id
			 },
			 success: function(response) {
				 var obj = Ext.decode(response.responseText);
				 obj.name = chatWin.name;
				 chatWin.chatHis.insert(0,obj);
				 chatWin.sendRequest({now: obj.chat_date});
			 }
		 });
		 
	 }
});
